// TBApi.cpp: implementation of the CTBApi class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "socclient.h"
#include "TBApi.h"
#include <afxinet.h> 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//#pragma comment(lib,"comsupp.lib")
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CTBApi::CTBApi()
{

}
CTBApi::CTBApi(CTBApiContext& ctx)
{
	SetContext(&ctx);
}
CTBApi::~CTBApi()
{
}

BOOL CTBApi::OpenURLGet(CString sURL, CString &strXML)
{
	CString myData;
	CString m_infor;
	CInternetSession mySession(NULL,0);
	CHttpFile* myHttpFile=NULL;
	m_url=sURL;	
	try
	{
		myHttpFile=(CHttpFile*)mySession.OpenURL(sURL);
		DWORD  len=myHttpFile->GetLength();
		while(myHttpFile->ReadString(myData))
		{
			m_infor=m_infor+_T("\r\n");
			m_infor+=myData;
		}
		myHttpFile->Close();
		mySession.Close();
	}
	catch(CInternetException* e)
	{
		e->ReportError();
		e->Delete();
		return FALSE;

	}
//	_bstr_t bstr;
//	ConvertUTF8toGB2312(m_infor,  bstr);
//	m_infor.Format (_T("%s"),(const char*)bstr);
	strXML=m_infor;

	m_xml=strXML;

	return TRUE;
}


void CTBApi::SetContext(CTBApiContext* pCtx)
{
	CString url,appkey,appsecret;
	url=pCtx->GetHJURL();
	pCtx->GetApp(appkey,appsecret);
	m_pa.SetHJURL(url);
	m_pa.SetApp(appkey,appsecret);

}


CPtrList* CTBApi::GetList()
{
	return &m_listItem;
}


void CTBApi::GetRetXML(CString &sURL, CString &sXML)
{
	sURL=m_url;
	sXML=m_xml;
}


//
//void CTBApi::ConvertGB2312toUTF8(const char *unistring, char *outbuffer, int len)
//{
//	wchar_t *buf = new wchar_t[len ];
//	MultiByteToWideChar(CP_ACP, 0, unistring, -1, buf, len);
//	WideCharToMultiByte(CP_UTF8, 0, buf, -1, outbuffer, len, NULL, NULL);
//	
//	delete []buf;
//}
//
//void CTBApi::ConvertUTF8toGB2312(CString str, _bstr_t &bstr)
//{
//	//LPCSTR lpStr = (LPCSTR)str;
//LPCSTR szTemp  =(LPCSTR)(LPCTSTR) str;
//	int n = MultiByteToWideChar(CP_UTF8, 0, szTemp, str.GetLength(), NULL, 0);
//
//
//	WCHAR *pChar = new WCHAR[n+1];
//	szTemp  =(LPCSTR)(LPCTSTR) str;
//	n = MultiByteToWideChar(CP_UTF8, 0, szTemp, str.GetLength(), pChar, n);
//	pChar[n] = 0;
//	OLECHAR *pTempBuffer = pChar;
//	BSTR strPassword = SysAllocString(pTempBuffer);
//	_bstr_t aa(strPassword, FALSE);
//	bstr = aa;
//	SysFreeString(strPassword);
//        delete [] pChar;
//}